package com.topica.pager;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.topica.R;
import com.topica.database.callback.OnGetListAnswerListener;
import com.topica.database.task.GetListAnswerTask;
import com.topica.object.AnswerItem;
import com.topica.object.QuestionItem;
import com.topica.pager.callback.OnChooseAnswerListener;

public class QuestionPager extends Fragment implements OnGetListAnswerListener,
		OnCheckedChangeListener {

	public final static String QUESTION_ITEM = "com.topica.pager.QuestionPager.QUESTION_ITEM";
	public final static String QUESTION_INDEX = "com.topica.pager.QuestionPager.QUESTION_INDEX";
	private static final String TAG = "QuestionPager";

	private View view;
	private TextView mNumberQuestion;
	private TextView mDescQuestion;
	private RadioGroup mGroupAnswers;
	private boolean didInit;
	private QuestionItem questionItem;
	private int index;
	private String mPreQuestionNumber;
	private boolean hasRequest;
	private int correctAnswer;
	private OnChooseAnswerListener chooseAnswerListener;
	private ArrayList<AnswerItem> dataAnswer;

	@SuppressLint("ValidFragment")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.layout_item_question, null);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		if (!didInit) {
			initVariables();
			didInit = true;
		}
		initControls();
		initActions();
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		chooseAnswerListener = (OnChooseAnswerListener) activity;
	}

	private void initVariables() {
		questionItem = (QuestionItem) getArguments().getSerializable(
				QUESTION_ITEM);
		index = getArguments().getInt(QUESTION_INDEX);
		mPreQuestionNumber = getActivity().getResources().getString(
				R.string.title_pre_question);
	}

	public void initControls() {
		mNumberQuestion = (TextView) view
				.findViewById(R.id.txtTitleNumberQuestion);
		mDescQuestion = (TextView) view.findViewById(R.id.txtTitleQuestion);
		mGroupAnswers = (RadioGroup) view.findViewById(R.id.radioAnswer);
		mGroupAnswers.setOnCheckedChangeListener(this);
	}

	public void initActions() {
		mNumberQuestion.setText(mPreQuestionNumber + " " + (index + 1));
		mDescQuestion.setText(questionItem.getQuestionText());
		if (index == 0 && !hasRequest) {
			requestList();
		}
	}

	@Override
	public void onGetListAnswerListener(ArrayList<AnswerItem> data) {
		hasRequest = true;
		Context context = getActivity().getApplicationContext();
		int indexAnswer = questionItem.getChooseAnswer();
		dataAnswer = data;
		for (int i = 0, n = data.size(); i < n; i++) {
			RadioButton button = new RadioButton(context);
			button.setTextColor(Color.BLACK);
			button.setTextSize(14);
			button.setId(i);
			button.setText(data.get(i).getAnswer());
			mGroupAnswers.addView(button);
			if (data.get(i).getFraction() == 1) {
				correctAnswer = data.get(i).getIdAnswer();
			}
			if (indexAnswer != -1 && indexAnswer == i) {
				button.setChecked(true);
			}
		}
		chooseAnswerListener.onCorrectAnswer(index, correctAnswer);
	}

	public void onPageChange() {
		if (!hasRequest) {
			requestList();
		}
	}

	private void requestList() {
		GetListAnswerTask task = new GetListAnswerTask(getActivity(), ""
				+ questionItem.getQuestionId());
		task.setOnGetListAnswerListener(this);
		task.execute();
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		chooseAnswerListener.onChooseAnswerListener(index, checkedId,dataAnswer.get(checkedId).getIdAnswer());
	}

	public static Fragment newInstance() {
		return new QuestionPager();
	}
}
